package com.myj.jim.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.JInternalFrame;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myj.jim.domain.Operator;
import com.myj.jim.domain.Role;
import com.myj.jim.domain.RoleAuthority;
import com.myj.jim.domain.bootstraptable.BootstrapTable;
import com.myj.jim.domain.bootstraptable.BootstrapTableJSON;
import com.myj.jim.service.RoleService;
import com.myj.jim.service.SysLogService;
import com.myj.jim.util.BootstrapTableUtil;
import com.myj.jim.util.JimUtil;

@RequestMapping("/role")
@Controller
public class RoleController {
	
	private static final String PAGE_AUTHORITY = "roleManage";
	
	private static final String NO_AUTHORITY_VIEW = "common/noAuthorityPage";
	
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private SysLogService sysLogService;
	
	@RequestMapping("/page")
	public String page(Model model, HttpSession session, HttpServletRequest req){
		String path = req.getContextPath();
		String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path+"/";
		Operator op = (Operator)session.getAttribute("operator");
		if(op!=null){
			if(JimUtil.haveAuthority(op, PAGE_AUTHORITY)){
				return "role/page";
			}else{
				return NO_AUTHORITY_VIEW;
			}
		}else{
			//到登录页面
			return "redirect:"+basePath+"index.jsp";
		}
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public BootstrapTableJSON<Role> list(BootstrapTable bt){
		Map<String,Object> map = BootstrapTableUtil.getParamMap(bt);
		return roleService.findPage(map);
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Role edit(Integer id){
		return roleService.edit(id);
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public Boolean save(Role role,String[] checkedAuthArr,HttpServletRequest req,HttpSession session){
		Operator op = (Operator)session.getAttribute("operator");
		boolean success = roleService.save(role, checkedAuthArr);
		if(success){
			JimUtil.insertLog(sysLogService, req, op, "20001", role.getRoleno(), role.getRolename(), "");
		}
		
		return success;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Boolean update(Role role, String[] checkedAuthArr,HttpServletRequest req,HttpSession session){
		Operator op = (Operator)session.getAttribute("operator");
		boolean success = roleService.update(role, checkedAuthArr);
		if(success){
			JimUtil.insertLog(sysLogService, req, op, "20002", role.getRoleno(), role.getRolename(), "");
		}
		return success;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Boolean delete(Integer id,HttpServletRequest req,HttpSession session){
		
		Operator op = (Operator)session.getAttribute("operator");
		Role r = roleService.getRoleById(id);
		boolean success = roleService.delete(id);
				
		if(success){
			JimUtil.insertLog(sysLogService, req, op, "20003", r.getRoleno(), r.getRolename(), "");
		}
		
		return success;
	}
	
	@RequestMapping("/isExist")
	@ResponseBody
	public Boolean isExist(String roleno){
		return roleService.isExist(roleno);
	
	}
	
	@RequestMapping("/getRoleAuthorityByRoleno")
	@ResponseBody
	public List<RoleAuthority> getRoleAuthorityByRoleno(String roleno){
		return roleService.selectRoleAuthorityByRoleno(roleno);
	}
}
